package com.bbc.action.user;

import org.springframework.beans.factory.annotation.Autowired;

import com.bbc.base.BaseAction;
import com.bbc.base.BaseMessage;
import com.bbc.entity.user.User;
import com.bbc.entity.user.UserQuery;
import com.bbc.service.user.UserService;
import com.bbc.util.PwdUtils;

public class UserAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	@Autowired
	private transient UserService userService;
	private User user;
	private UserQuery query;
	private String fromUrl = "success";
	
	public String login() {
		return "login";
	}
	
	public String reg() {
		return "reg";
	}
	
	public String dologin() {
		user = userService.login(user);
		if (user == null) {
			BaseMessage m = new BaseMessage();
			m.setSuccess(false);
			m.setCode(104);
			m.setInfo("用户名或密码错误");
			setMsg(m);
			return "login";
		}
		setU(user);
		return fromUrl;
	}
	
	public String doreg() {
		user.setPwd(PwdUtils.build(user.getPassword()));
		userService.addUser(user);
		return dologin();
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserQuery getQuery() {
		return query;
	}

	public void setQuery(UserQuery query) {
		this.query = query;
	}

	public String getFromUrl() {
		return fromUrl;
	}

	public void setFromUrl(String fromUrl) {
		this.fromUrl = fromUrl;
	}

}
